const User = require("../model/user.model");

class UserService {
  async createUser(user_name, password) {
    //? todo: 插入数据
    const res = await User.create({
      //? 表的字段
      user_name: user_name,
      password: password,
    });
    //* console.log(res);
    console.log(res.dataValues);
    return res.dataValues;
  }

  async getUserInfo({ id, user_name, password }) {
    const whereOpt = {};

    id && Object.assign(whereOpt, { id });
    user_name && Object.assign(whereOpt, { user_name });
    password && Object.assign(whereOpt, { password });

    const res = await User.findOne({
      attributes: [
        "id",
        "user_name",
        "password",
        "user_gendar",
        "phone_number",
        "user_signature",
        "user_avatar",
      ],
      where: whereOpt,
    });
    /*   const res = await User.findOne({
      where: {
        user_name: user_name,
      },
    }); */

    console.log(res);

    return res ? res.dataValues : null;
  }

  async updateById({ id, user_name, password, is_admin }) {
    const whereOpt = { id };

    const newUser = {};

    user_name && Object.assign(newUser, { user_name });
    password && Object.assign(newUser, { password });
    is_admin && Object.assign(newUser, { is_admin });

    const res = await User.update(newUser, { where: whereOpt });
    //!console.log(res);
    return res[0] > 0 ? true : false;
  }

  async updateUserInfo(id, user_update_info) {
    console.log(user_update_info);
    const res = await User.update(
      {
        user_name: user_update_info.user_name,
        user_avatar: user_update_info.user_avatar,
        user_gendar: user_update_info.user_gendar,
        user_signature: user_update_info.user_signature,
      },
      {
        where: {
          id: id,
        },
      }
    );
    return res[0] > 0 ? true : false;
  }
}

module.exports = new UserService();
